﻿#ifndef LOGGER_H
#define LOGGER_H

#include <QObject>
#include <QThread>
#include <QMap>
#include <QDebug>
#include <QQueue>
#include <QMutex>
#include <QMutexLocker>

#define qtdebug qDebug()
#define qtinfo qInfo()
#define qtcritical qCritical()
#define qtwarning qWarning()

enum TextColor{
    TextBlack = 0,
    TextRed,
    TextGreen,
    TextBlue,
    TextOrange,
};
const QMap<int, QString> TextColorMap = {
    {TextBlack,"black"},
    {TextRed,"red"},
    {TextGreen,"green"},
    {TextBlue,"blue"},
    {TextOrange,"orange"},
};
QString ColorTextStr(QString, quint8);
// 饿汉单例模式
class Logger : public QThread
{
    Q_OBJECT
private:
    explicit Logger(){};

    Logger(const Logger&) = delete;; //拷贝构造

    Logger& operator=(const Logger&) = delete;;  //=运算符重载
protected:
    void run() override;
public:
    static Logger& Instance()
    {
        static Logger LoggerInstance; // 饿汉单例，先创建实例，线程安全，必须用static修饰
        return LoggerInstance;
    }
    void stopRun() { running = false; }

    void Info(const QString& msg);
    void Bingo(const QString& msg);
    void Critical(const QString& msg);
    void Warning(const QString& msg);
    void Error(const QString& msg);

    QString getLogFileName() const;
    void setLogFileName(const QString &newLogFileName);

    QString getLogFilePath() const;
    bool setLogFilePath(const QString &newLogFilePath);

signals:
    void log_msg(const QString& msg);
    void err_occur(const QString& msg);

private:
    QQueue<QString> logQueue;
    QMutex logMutex;
    QString logFileName = "tempLog.txt";
    QString logFilePath = "";
    bool running = false;
};

#endif // LOGGER_H
